<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="cn">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.0.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.0.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.0.0" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="categories">
<meta property="og:url" content="http://yoursite.com/categories/index.html">
<meta property="og:site_name" content="hill&#39;s blog">
<meta property="og:locale" content="cn">
<meta property="article:published_time" content="2017-07-10T08:36:26.000Z">
<meta property="article:modified_time" content="2021-02-25T04:04:48.836Z">
<meta property="article:author" content="heyaojia">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/categories/"/>





  <title> | hill's blog</title>
  








<meta name="generator" content="Hexo 4.2.1"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="cn">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">hill's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    
    
    
    <div class="post-block page">
      <header class="post-header">

	<h1 class="post-title" itemprop="name headline"></h1>



</header>

      
      
      
      <div class="post-body">
        
        
          <h3 id="MYSQL-MVCC"><a href="#MYSQL-MVCC" class="headerlink" title="MYSQL -MVCC"></a><strong><em>MYSQL</em></strong> -MVCC</h3><hr>
<h4 id="一、MVCC基础描述"><a href="#一、MVCC基础描述" class="headerlink" title="一、MVCC基础描述"></a>一、MVCC基础描述</h4><p>​        <font size=6>“</font><code>MVCC（Muti-Version Concurrency Control），意思是符合版本并发并发控制。是一种控制并发的方法。</code><font size=6>“</font></p>
<ul>
<li>学习资料 <a href="https://www.jianshu.com/p/8845ddca3b23" target="_blank" rel="noopener">https://www.jianshu.com/p/8845ddca3b23</a></li>
</ul>
<h5 id="什么是当前读和快照读？"><a href="#什么是当前读和快照读？" class="headerlink" title="什么是当前读和快照读？"></a>什么是当前读和快照读？</h5><p>​        学习<strong>MVCC</strong>之前，要搞懂的两个概念。</p>
<ul>
<li><p>当前读（悲观锁）</p>
<p>当前读的意思就是，读最新的那一个版本。如果保证当前这一时刻读的是最新的那一行呢，只能是锁住这一行，不允许其他操作来更新这行记录，那么当前这个时刻就是最新的，Mysql这里用的就是加锁。</p>
<ul>
<li><p>共享锁</p>
<p>select lock in share mode；</p>
</li>
<li><p>排他锁</p>
<p>select for update；insert；delete</p>
</li>
</ul>
</li>
<li><p>快照读</p>
<p>快照读，顾名思义，就是把当前行，copy一份出来。快照都的前提是隔离级别不能是串行级别，如果是串行，那么跟当前都无异。那么快照读为了性能不加锁，极有可能有别的事务操作来修改了这一行，所以有可能读的是历史数据版本。</p>
</li>
</ul>
<ul>
<li><strong>MVCC</strong>就是为了是写读写冲突不加锁，读指的是快照读。当前读、快照读和MVCC的关系就是，<strong>MVCC</strong>为了“实现一个数据的多个版本，使的读写不冲突”这个理想状态。MVCC模型在MYSQL中的具体实现是三个隐式字段，undo日志、Read View 等完成的。</li>
</ul>
<h5 id="MVCC好处："><a href="#MVCC好处：" class="headerlink" title="MVCC好处："></a>MVCC好处：</h5><h5 id="数据库并发场景分别是："><a href="#数据库并发场景分别是：" class="headerlink" title="数据库并发场景分别是："></a>数据库并发场景分别是：</h5><ul>
<li><strong><em>读 - 读：</em></strong>不存在并发问题，不需要并发控制</li>
<li><strong><em>读 - 写：</em></strong>有现成安全问题，有可能脏读、幻读、不可重复读</li>
<li><strong><em>写- 写：</em></strong> 线程安全问题，更新丢失问题</li>
</ul>
<p><strong>MVCC</strong>可以解决幻读、脏读、不可重复读问题，但是<strong>MVCC</strong>不能解决更新丢失问题。可以使用以下组合来解决问题：</p>
<ul>
<li>MVCC + 悲观锁<br> MVCC解决读写冲突，悲观锁解决写写冲突</li>
<li>MVCC + 乐观锁<br> MVCC解决读写冲突，乐观锁解决写写冲突。这种组合的方式就可以最大程度的提高数据库并发性能，并解决读写冲突，和写写冲突导致的问题</li>
</ul>
<h4 id="二、MVCC-实现原理"><a href="#二、MVCC-实现原理" class="headerlink" title="二、MVCC 实现原理"></a>二、<strong>MVCC</strong> 实现原理</h4><h5 id="隐式字段："><a href="#隐式字段：" class="headerlink" title="隐式字段："></a>隐式字段：</h5><p><code>DB_TRX_ID,DB_ROLL_PTR,DB_ROW_ID</code></p>
<ul>
<li>DB_TRX_ID<br> 6byte，最近修改(修改/插入)事务ID：记录创建这条记录/最后一次修改该记录的事务ID</li>
<li>DB_ROLL_PTR<br> 7byte，回滚指针，指向这条记录的上一个版本（存储于rollback segment里）</li>
<li>DB_ROW_ID<br> 6byte，隐含的自增ID（隐藏主键），如果数据表没有主键，InnoDB会自动以DB_ROW_ID产生一个聚簇索引</li>
</ul>
<p>实际还有一个删除flag隐藏字段, 既记录被更新或删除并不代表真的删除，而是删除flag变了</p>
<h5 id="undo日志："><a href="#undo日志：" class="headerlink" title="undo日志："></a>undo日志：</h5><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
        
      </div>
      
      
      
    </div>
    
    
    
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">heyaojia</p>
              <p class="site-description motion-element" itemprop="description">游戏行业的一枚小鲜肉</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="git@gitee.com:hillcool/hillcool.git" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-gitee"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#MYSQL-MVCC"><span class="nav-number">1.</span> <span class="nav-text">MYSQL -MVCC</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#一、MVCC基础描述"><span class="nav-number">1.1.</span> <span class="nav-text">一、MVCC基础描述</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#什么是当前读和快照读？"><span class="nav-number">1.1.1.</span> <span class="nav-text">什么是当前读和快照读？</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#MVCC好处："><span class="nav-number">1.1.2.</span> <span class="nav-text">MVCC好处：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#数据库并发场景分别是："><span class="nav-number">1.1.3.</span> <span class="nav-text">数据库并发场景分别是：</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#二、MVCC-实现原理"><span class="nav-number">1.2.</span> <span class="nav-text">二、MVCC 实现原理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#隐式字段："><span class="nav-number">1.2.1.</span> <span class="nav-text">隐式字段：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#undo日志："><span class="nav-number">1.2.2.</span> <span class="nav-text">undo日志：</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heyaojia</span>

  

  
</div>









        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.0.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.0.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.0.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.0.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.0.0"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
